// import axios from '@/utils/request'
var format = require('date-format')
// const SETTLE_STATUS = {
//   0: '未分账',
//   1: '已分账'
// }
const TYPES = {
  1: '下单人佣金',
  2: '支付手续费',
  3: '平台佣金',
  4: '门店收入',
  100: '剩余余额',
  999: '平台手续费'
}
export default {
  SettleItem: {
    form: {
      fields: [
        'status',
        'paidTime'
      ]
    },
    list: {
      export: {
        query: {
          '@display': `{
                      'outTradeNo': 'entity.getSettle().getOrderItem().getOrder().getInvoice().getOutTradeNo()',
                      'product' : 'entity.getSettle().getOrderItem().getSpecification().getProduct().getName()',
                      'quantity' : 'entity.getSettle().getOrderItem().getQuantity()',
                      'price' : 'entity.getSettle().getOrderItem().getPrice()',
                      'profitType' : '{1: "下单人佣金",2: "支付手续费",3: "平台佣金",4: "核销人佣金",100: "剩余余额",999: "平台手续费"}[entity.getProfitType()]',
                      'amount' : 'entity.getAmount()',
                      'acc': 'entity.getStaff() ? entity.getStaff().__toString() : entity.getUser().__toString()',
                      'settled' : '{0: "未分账",1: "已分账"}[entity.getSettled()]',
                      'orderTime' : 'entity.getSettle().getOrderItem().getOrder().getCreatedTime().format("Y-m-d H:i:s")',
                      'createdTime' : 'entity.getCreatedTime().format("Y-m-d H:i:s")',
                      'status': 'entity.getSettle().getOrderItem().getOrder().getStatus()'
                  }`,
          'limit': 100000000
        },
        label: {
          'outTradeNo': '支付单号',
          'product': '产品',
          'quantity': '数量',
          'price': '总价',
          'profitType': '类型',
          'amount': '分账金额',
          'acc': '分账账号',
          'settled': '分账状态',
          'orderTime': '下单时间',
          'createdTime': '分账时间',
          'status': '订单状态'
        }
      },
      disabled_actions: ['new', 'edit', 'export'],
      query: {
        '@order': 'entity.id|DESC',
        '@expands': "['entity.settle.orderItem.order','entity.settle.orderItem.specification']"
      },
      list_display: [
        { property: 'settle.__metadata.orderItem.__metadata.specification.__metadata.product.__toString', label: '产品' },
        { property: 'settle.__metadata.orderItem.__metadata.quantity', label: '数量' },
        { property: 'settle.__metadata.orderItem.__metadata.price', label: '总价' },
        {
          property: 'profitType', label: '类型',
          component: {
            props: ['data'],
            data() {
              return {
                types: TYPES
              }
            },
            render(h) {
              return (
                <span>{this.types[this.data]}</span>
              )
            }
          }
        },
        {
          property: 'amount', label: '分账金额',
          component: {
            props: ['data'],
            render(h) {
              return <span>￥{this.data}</span>
            }
          }
        },
        {
          property: 'acc', label: '分账账号',
          component: {
            props: ['record'],
            render(h) {
              if (this.record.staff) {
                return <span>{this.record.staff.__toString}</span>
              } else {
                return <span>{this.record.user.__toString}</span>
              }
            }
          }
        },
        {
          property: 'settled', label: '分账状态',
          component: {
            props: ['data', 'record'],
            render(h) {
              if (this.data) {
                return <el-tag type='success'>已分账</el-tag>
              } else {
                return <el-tag type='warning'
                  onClick={() => {
                    console.log(this.record)
                    const orderId = this.record.settle.__metadata.orderItem.__metadata.order.id
                    console.log(orderId)
                    this.axios.post(`/manage/orders/${orderId}/settle-execute`).then((res) => {
                      console.log(res.data)
                      this.$message.success('分账成功！')
                      this.record.settled = true
                    })
                  }}
                >未分账</el-tag>
              }
            }
          }
        },
        {
          property: 'settle.__metadata.orderItem.__metadata.order.__metadata.createdTime',
          label: '订单创建时间',
          component: {
            props: ['data'],
            data() {
              return { createdTime: format('yyyy-MM-dd hh:mm:ss', new Date(this.data)) }
            },
            render(h) {
              return <span>{this.createdTime}</span>
            }
          }
        },
        'createdTime'
      ]
    }
  }
}

